﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Leuphana.GuidedTour.App.ViewModels
{
    public class ArticleViewModel : ViewModelBase
    {
        // Fields
        private int _id;
        private string _title;
        private string _description;
        private string _text;
        private bool _isDataLoaded = false;

        private int _top;
        private int _left;

        // Constructor
        public ArticleViewModel()
        {
            Images = new ObservableCollection<string>();
            Images.CollectionChanged += Images_CollectionChanged;

            NavigateToCommand = new NavigationCommand("article", "id");
        }

        void Images_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (Images.Count > 1)
            {
                NavigateToCommand = new NavigationCommand("slider", "id");
            }
            else
            {
                NavigateToCommand = new NavigationCommand("article", "id");
            }

            OnPropertyChanged("NavigateToCommand");
        }

        // Collection properties
        public ObservableCollection<string> Images { get; private set; }

        // Data properties
        public int ID
        {
            get { return _id; }
            set { if (value != _id) { _id = value; OnPropertyChanged("ID"); } }
        }
        public string Title
        {
            get { return _title; }
            set { if (value != _title) { _title = value; OnPropertyChanged("Title"); } }
        }
        public string Description
        {
            get { return _description; }
            set { if (value != _description) { _description = value; OnPropertyChanged("Description"); } }
        }
        public string Text
        {
            get { return _text; }
            set { if (value != _text) { _text = value; OnPropertyChanged("Text"); } }
        }
        public string ImageSource
        {
            get { return Images.FirstOrDefault(); }
        }

        public int Top
        {
            get { return _top; }
            set { if (value != _top) { _top = value; OnPropertyChanged("Top"); } }
        }
        public int Left
        {
            get { return _left; }
            set { if (value != _left) { _left = value; OnPropertyChanged("Left"); } }
        }

        // Commands
        public ICommand NavigateToCommand { get; private set; }

        // Methods
        public void LoadData(int id)
        {
            // Only load once
            if (_isDataLoaded)
                return;

            // Load item from xml.
            var data = TourDataLoader.Current.LoadArticle(id.ToString());

            // Set data.
            _id = id;
            _title = data.Element("Title").Value;
            _description = data.Element("Description").Value;
            _text = data.Element("Text").Value.TrimStart(new char[] {' '});
            _top = Int32.Parse(data.Element("Top").Value);
            _left = Int32.Parse(data.Element("Left").Value);

            // Grap all images.
            foreach (var item in (from image in data.Element("Images").Elements("Image")
                                  select image.Value))
            {
                Images.Add("/Images/" + item);
            }

            // Update all bindings.
            _isDataLoaded = true;
            OnPropertyChanged(String.Empty);
        }
    }
}
